package com.tbit.main.controller;



import com.alibaba.fastjson.JSON;
import com.tbit.common.constant.CommonConstant;
import com.tbit.main.common.JsonResult;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import com.tbit.manager.api.QuickReplyApi;
import com.tbit.manager.api.dto.QuickReplyDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * 快捷回复表(QuickReply)表控制层
 *
 * @author shanyong.mo
 * @since 2025-02-11 10:09:05
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/quickReply")
public class QuickReplyController {
    /**
     * 服务对象
     */
    @Autowired
    private QuickReplyApi quickReplyApi;

    @Autowired
    private TokenService tokenService;

    /**
     * 分页查询
     * @param pageNum 页码
     * @param pageSize 页长
     * @param search 搜索内容
     * @return 查询结果
     */
    @GetMapping("/queryByPage")
    public JsonResult queryByPage(String token,Integer pageNum, Integer pageSize, String search) {
        AccountUser accountUser = tokenService.getAccount(token);
        QuickReplyDTO quickReplyDTO = new QuickReplyDTO();
        quickReplyDTO.setAccountId(accountUser.getAccountId());
        quickReplyDTO.setUserId(accountUser.getAccountUserId());
        quickReplyDTO.setPlatform(CommonConstant.QUICK_REPLY_PLATFORM_MANAGER_OR_MAIN);
        quickReplyDTO.setPageNum(pageNum);
        quickReplyDTO.setPageSize(pageSize);
        quickReplyDTO.setSearch(search);
//        log.info("queryByPage-------->quickReplyDTO:{}",JSON.toJSONString(quickReplyDTO));
        Map<String, Object> map = quickReplyApi.queryByPage(JSON.toJSONString(quickReplyDTO));
        return JsonResult.succ((Integer) map.get("total"),map.get("data"));
    }

    /**
     * 新增数据
     *
     * @param quickReplyDTO 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public JsonResult add(String token, QuickReplyDTO quickReplyDTO) {
        Assert.notNullCheckLength(quickReplyDTO.getQuickReply(),CommonConstant.QUICK_REPLY_MAX_LENGTH,"快捷回复内容为空或超过指定长度200字符");
        AccountUser accountUser = tokenService.getAccount(token);
        quickReplyDTO.setUserId(accountUser.getAccountUserId());
        quickReplyDTO.setPlatform(CommonConstant.QUICK_REPLY_PLATFORM_MANAGER_OR_MAIN);
        quickReplyDTO.setAccountId(accountUser.getAccountId());
//        log.info("add-------->quickReplyDTO:{}",JSON.toJSONString(quickReplyDTO));
        quickReplyApi.add(quickReplyDTO);
        return JsonResult.succ();
    }

    /**
     * 编辑数据
     *
     * @param quickReplyDTO 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public JsonResult edit(QuickReplyDTO quickReplyDTO) {
        Assert.notNullCheckLength(quickReplyDTO.getQuickReply(),CommonConstant.QUICK_REPLY_MAX_LENGTH,"快捷回复内容为空或超过指定长度200字符");
        Assert.notNull(quickReplyDTO.getId(),"找不到修改主键");
//        log.info("edit-------->quickReplyDTO:{}",JSON.toJSONString(quickReplyDTO));
        quickReplyApi.edit(quickReplyDTO);
        return JsonResult.succ();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("/delete")
    public JsonResult deleteById(Integer id) {
        quickReplyApi.deleteById(id);
        return JsonResult.succ();
    }

}

